用Python绘制ROC曲线 您所在的位置:网站首页 matlab plotroc函数 用Python绘制ROC曲线

用Python绘制ROC曲线

#用Python绘制ROC曲线| 来源: 网络整理| 查看: 265

       在分类模型中,ROC曲线和AUC值经常作为衡量一个模型拟合程度的指标。最近在建模过程中需要作出模型的ROC曲线,参考了sklearn官网的教程和博客。现在将自己的学习过程总结如下,希望对初次接触的同学有所帮助。PS:网上的例子实在是晦涩难懂,在折腾了一下午之后终于搞定了。下面是我在学习过程中主要参考的资料,大家也可以学习一下。

http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html

http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html

http://scikit-learn.org/stable/modules/generated/sklearn.metrics.auc.html

http://blog.csdn.net/u010454729/article/details/45098305

      sklearn上有一个画ROC曲线的例子,利用的是经典的鸢尾花(iris)数据。但鸢尾花数据分类的结果有三种,例子就直接来做图(一般的分类任务明明只有两种结果啊!!!),对于初学者来说(说的是我自己)看起来真的别扭。因此我对该例子做了一些改动(简化),将数据转化为二分类,这样比较容易理解。

       首先为大家介绍一下Python做ROC曲线的原理。sklearn.metrics有roc_curve, auc两个函数,ROC曲线上的点主要就是通过这两个函数计算出来的。

(1) fpr, tpr, thresholds  =  roc_curve(y_test, scores) 

      其中y_test为测试集的结果,scores为模型预测的测试集得分(注意:通过decision_function(x_test)计算scores的值);fpr,tpr,thresholds 分别为假正率、真正率和阈值。(应该是不同阈值下的真正率和假正率)。

(2) roc_auc =auc(fpr, tpr) 

roc_auc为计算的acu的值。

PS:下面的代码大家可以直接copy跑一下,我相信跑完之后大家就理解作图的原理了。

# -*- coding: utf-8 -*- """ Created on Thu Sep 21 16:13:04 2017 @author: lizhen """ import numpy as np import matplotlib.pyplot as plt from sklearn import svm, datasets from sklearn.metrics import roc_curve, auc ###计算roc和auc from sklearn import cross_validation # Import some data to play with iris = datasets.load_iris() X = iris.data y = iris.target ##变为2分类 X, y = X[y != 2], y[y != 2] # Add noisy features to make the problem harder random_state = np.random.RandomState(0) n_samples, n_features = X.shape X = np.c_[X, random_state.randn(n_samples, 200 * n_features)] # shuffle and split training and test sets X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=.3,random_state=0) # Learn to predict each class against the other svm = svm.SVC(kernel='linear', probability=True,random_state=random_state) ###通过decision_function()计算得到的y_score的值,用在roc_curve()函数中 y_score = svm.fit(X_train, y_train).decision_function(X_test) # Compute ROC curve and ROC area for each class fpr,tpr,threshold = roc_curve(y_test, y_score) ###计算真正率和假正率 roc_auc = auc(fpr,tpr) ###计算auc的值 plt.figure() lw = 2 plt.figure(figsize=(10,10)) plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve (area = %0.2f)' % roc_auc) ###假正率为横坐标,真正率为纵坐标做曲线 plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristic example') plt.legend(loc="lower right") plt.show()

代码结果如下:

def acu_curve(y,prob): fpr,tpr,threshold = roc_curve(y,prob) ###计算真正率和假正率 roc_auc = auc(fpr,tpr) ###计算auc的值 plt.figure() lw = 2 plt.figure(figsize=(10,10)) plt.plot(fpr, tpr, color='darkorange', lw=lw, label='ROC curve (area = %0.3f)' % roc_auc) ###假正率为横坐标,真正率为纵坐标做曲线 plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Receiver operating characteristic example') plt.legend(loc="lower right") plt.show()

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有